package com.springMVC.controller;

import com.springMVC.service.UserService;
import com.springMVC.util.MD5code;
import com.springMVC.util.Rest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * Created by aaa on 2015-07-23.
 */

@Controller
public class UserController{
    private static ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:conf/spring.xml"
            , "classpath:conf/spring-mybatis.xml"});
    private static UserService userService
            =(UserService) context.getBean("userServiceImpl");

    /**
     * 跳转到login页面
     * @return
     */
    @RequestMapping("login")
    public String login(){
        return "index";
    }

    /**
     * 登录成功跳转到首页
     * @param modelMap
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("main")
    public String main(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response){
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        //modelMap.addAttribute("user",modelMap.addAttribute("user",userLoginService.checkNamePwd(username,password)));
        request.getSession().setAttribute("userName", userService.checkNamePwd(username,MD5code.GetMD5Code(password)).getName());
        return "orderList";
    }

    /**
     * 帐号密码检查
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("checkLogin")
    @ResponseBody
    public Rest checkLogin(HttpServletRequest request,HttpServletResponse response){
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        Rest rest=new Rest();
        if(username==null||password==null||username.equals("")||password.equals("")){
            rest.setIsSuccess(false);
            rest.setError("用户名或密码不能为空！");
            return rest;
        }
        if(userService.checkNameExist(username)!=null){
            if(userService.checkNamePwd(username, MD5code.GetMD5Code(password))!=null){
                rest.setIsSuccess(true);
                rest.setMessage("登录系统成功！");
            }else{
                rest.setIsSuccess(false);
                rest.setError("账号或密码错误！");
            }
        }else{
            rest.setIsSuccess(false);
            rest.setError("用户名不存在或已被删除！");
        }
        return rest;
    }
    /**
     * 跳转到注册页面
     * @return
     */
    @RequestMapping("register")
    public String register(){
        return "register";
    }
    /**
     * 跳转到主页
     * @return
     */
    @RequestMapping("home")
    public String home(){
        return "home";
    }
}
